
package com.innovamed.websocket.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.innovamed.websocket.model.WsConfigInfo;
import com.innovamed.websocket.model.WsMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.innovamed.websocket.handler.SocketServer;
import com.innovamed.websocket.service.WsService;
import org.springframework.web.bind.annotation.RestController;


/** Copyright: Copyright (c) 2019 Jun_Zhou
 * 
 * @ClassName: WsController.java
 * @Description: websocket请求处理类;
 * @version: v1.0.0
 * @author: JunZhou
 * @Email: 1769676159@qq.com
 * @Site: CERNO
 * @date: 2019年1月16日 下午2:06:21 */
@RestController
@RequestMapping("/api/ws/")
public class WsController
{  
	
	@Autowired
	public WsService wsService;
	
	/** ws测试接口;
	 * 
	 * @return:测试字符串;
	 * 
	 */
	@RequestMapping("sockettest")
	public String sockettest()
	{
		return "sockettest";
	}
	
	/** 统计数据;
	 * 
	 * @return:在线用户总数+在线用户userId的拼接字符串; 
	 * 
	 */
	@RequestMapping("statistic")
	public String statistic()
	{
		Integer onlineTotal = SocketServer.getOnlineNum();//当前在线用户的总数;
		String onlineUsers = SocketServer.getOnlineUsers();//当前在线所有用户的userId拼接字符串;
		String statistic = onlineTotal + "&&" + onlineUsers;
		return statistic;
	}
	
	/** 发送/推送消息到指定用户;
	 * 
	 * @param request
	 * @return:发送/推送结果【success/failure】; 
	 * 
	 */
	@RequestMapping("sendmsg")
	@ResponseBody
	public String sendmsg(HttpServletRequest request,@RequestBody WsMsg msg)
	{
		//String username = request.getParameter("username");
		//String msg = request.getParameter("msg");
		String receiver = msg.getReceiver();
		String msgBody = msg.getMsgBody();
		SocketServer.sendMessage(msgBody, receiver);
		return "success";
	}
	
	/** 发送/推送消息到所有在线用户;
	 * 
	 * @param request
	 * @return:发送/推送结果【success/failure】; 
	 * 
	 */
	@RequestMapping("sendAll")
	@ResponseBody
	public String sendAll(HttpServletRequest request,@RequestBody WsMsg msg)
	{
		//String msg = request.getParameter("msg");
		String msgBody = msg.getMsgBody();
		SocketServer.sendAll(msgBody);
		return "success";
	}
	
	/** 发送/推送消息到指定的在线用户;
	 * 
	 * @param request
	 * @return:发送/推送结果【success/failure】; 
	 * 
	 */
	@RequestMapping("sendMsgToSpecifiedRange")
	@ResponseBody
	public String sendMsgToSpecifiedRange(HttpServletRequest request,HttpServletResponse response,@RequestBody WsMsg wsMsg)
	{
		wsService.sendMsgToSpecifiedRange(request,wsMsg);
		return "success";
	}
	
	/** 
	  *   获取WS服务的配置信息;
	 * @param request
	 * @return:发送/推送结果【success/failure】; 
	 * 
	 */
	@RequestMapping("getWsConfig")
	@ResponseBody
	public WsConfigInfo getWsConfig(HttpServletRequest request, HttpServletResponse response)
	{
		WsConfigInfo wsConfigInfo = wsService.getWsConfig(request);
		return wsConfigInfo;
	}
	
}
